﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Estsh.Core.Base;
using Estsh.Core.Wms.IRepositories;
using Estsh.Core.Wms.IServices;
using Estsh.Core.Model.Result;
using Estsh.Core.Services;
using Estsh.Core.Repositories;
using Estsh.Core.Models;
using Newtonsoft.Json.Linq;
using Estsh.Core.Wms.Repositories;
using Estsh.Core.Model.EnumUtil;
using Dapper;
using System.Security.Policy;
using System.Data;
using Newtonsoft.Json;
using SqlSugar;

using static Estsh.Core.Model.EnumUtil.WmsEnumUtil;
using Estsh.Core.Model.Extend.Models;

namespace Estsh.Core.Wms.Services
{
    public class ReceiveService : BaseService<BaseEntity>, IReceiveService
	{
		private readonly IReceiveRepository repository;
		public ReceiveService(IReceiveRepository _repository) : base(_repository)
		{
			repository = _repository;
		}
		public string UpdateCartonStatusInRejection(string cartonNo, string info, string loginId, string supportingParts)
		{
			return repository.UpdateCartonStatusInRejection(cartonNo, info, loginId, supportingParts);
		}

		public string UpdateCartonStatusInRejectionBycd(string cartonNo, string info, string loginId, string supportingParts, string cdNum)
		{
			return repository.UpdateCartonStatusInRejectionBycd(cartonNo, info, loginId, supportingParts, cdNum);
		}
		/// <summary>
		/// 检查箱条码的状态
		/// </summary>
		/// <param name="cartonNo">箱条码</param>
		/// <returns>状态正确返回 OK，否则返回具体的错误信息</returns>
		public string CheckCartonStatus(string cartonNo, bool isRejection, string groupNo, string loginId, string supportingParts, string cdNum, string orderNo, bool isQc)
		{
			return repository.CheckCartonStatus(cartonNo, isRejection, groupNo, loginId, supportingParts, cdNum, orderNo, isQc);

		}

		public SetObjectDetail GetCartonSummaryInfo(string cartonNo, string receving, string supportingParts, string loginId)
		{
			return repository.GetCartonSummaryInfo(cartonNo, receving, supportingParts, loginId);
		}
		public SetObjectDetail GetCartonSummaryInfoNew(string cartonNo, string receving, string supportingParts, string loginId,string locateName)
		{
			return repository.GetCartonSummaryInfoNew(cartonNo, receving, supportingParts, loginId, locateName);
		}

		public SetObjectDetail GetPurchaseSummaryInfo(string cartonNo, string receving, string supportingParts, string loginId ,string nstock)
		{
			return repository.GetPurchaseSummaryInfo(cartonNo, receving, supportingParts, loginId, nstock);
		}

		public SetObjectDetail GetRecevingListing(string cartonNo)
		{
			return repository.GetRecevingListing(cartonNo);
		}

		public string CreateQcInfo(string receving, string loginId)
		{
			return repository.CreateQcInfo(receving, loginId);
		}

		public List<WmsRejection> GetRejectionInfo()
		{
			return repository.GetRejectionInfo();
		}

		public string CheckStatus(string orderNo, string loginId)
		{
			return repository.CheckStatus(orderNo, loginId);
		}
		public string CheckPurchaseStatus(string receving, string loginId)
		{
			return repository.CheckPurchaseStatus(receving, loginId);
		}

		public List<WmsPurchase> GetStandardList(string cartonno)
		{
			return repository.GetStandardList(cartonno);
		}

		public SetObjectDetail GetRecevingListByOrderNo(string orderNo,string cartonNo)
		{
			return repository.GetRecevingListByOrderNo(orderNo, cartonNo);
		}

		public SetObjectDetail GetRealOrderList(string orderNo)
		{
			return repository.GetRealOrderList(orderNo);
		}

		/// <summary>
		/// 根据登录人id查询默认库位
		/// </summary>
		/// <param name="loginId"></param>
		/// <returns></returns>
		public SetObjectDetail GetLogIdByLocateName(string loginId)
		{
			SetObjectDetail rfInfo = new SetObjectDetail();//返回消息

			List<SysLocate> sysLocates = repository.GetLogIdByLocateName(loginId);

			if (sysLocates.Count == 0)
			{
				//提示  [登录人]:登录人没有默认的库位，请确认!
				rfInfo.type = "FAIL";
				rfInfo.message = "登录人没有默认的库位，请确认!";
				return rfInfo;
			}

			rfInfo.type = "PASS";
			rfInfo.message = sysLocates[0].LocateName;//库位
			return rfInfo;
		}


		/// <summary>
		/// 验证库位
		/// </summary>
		/// <param name="locateName">库位名称</param>
		/// <param name="loginId">登录人id</param>
		/// <returns></returns>
		public SetObjectDetail GetLocateByName(string locateName, string loginId)
		{
			List<SysLocate> sysLocates = repository.GetLocateByName(locateName, loginId);

			List<SysStock> sysStocksLocateCount = repository.GetStockQtyByLocate(locateName);//库位在库数

			SetObjectDetail rfInfo = new SetObjectDetail();//返回消息
			if (sysLocates.Count == 0)
			{
				//提示  [A1-2-2]:该库位不存在，请检查!
				rfInfo.type = "FAIL";
				rfInfo.message = "[" + locateName + "]:该库位不存在，请检查!";
				return rfInfo;
			}
			else if (sysLocates[0].Enabled != "Y")
			{
				//提示  [A1-2-2]:该库位不可用，请检查!
				rfInfo.type = "FAIL";
				rfInfo.message = "[" + locateName + "]:该库位不可用，请检查!";
				return rfInfo;
			}
			//else if (sysLocates[0].LocateType != (int)WmsEnumUtil.LocateType.BASICS_LOCATE && sysLocates[0].LocateType != (int)WmsEnumUtil.LocateType.MANEUVER_LOCATE && sysLocates[0].LocateType != (int)WmsEnumUtil.LocateType.COMBINATION_LOCATE)
			else if (sysLocates[0].LocateType == (int)WmsEnumUtil.LocateType.LINE_LOCATE || sysLocates[0].LocateType == (int)WmsEnumUtil.LocateType.COLLECT_LOCATE || sysLocates[0].LocateType == (int)WmsEnumUtil.LocateType.NC_LOCATE)
			{
				//提示  [A1-2-2]:该库位不可用，请检查!
				rfInfo.type = "FAIL";
				rfInfo.message = "[" + locateName + "]:该库位为" + sysLocates[0].LocateTypeDesc + "，请检查!";
				return rfInfo;
			}
			else if ((decimal)sysStocksLocateCount[0].Qty >= sysLocates[0].LocateCapacity)//当前库位条码数量大于等于当前库位容量
			{
				//提示  [A1-2-2]:该库位容量已满，请检查!
				rfInfo.type = "FAIL";
				rfInfo.message = "[" + locateName + "]:该库位容量已满，请检查!";
				return rfInfo;
			}

			rfInfo.type = "PASS";
			rfInfo.message = "[" + locateName + "]:扫描成功,请扫描箱条码";
			return rfInfo;
		}

		/// <summary>
		/// 正常收货
		/// </summary>
		/// <param name="cartonNo">箱条码</param>
		/// <param name="isRejection">是否拒收</param>
		/// <param name="groupNo">组托uuid</param>
		/// <param name="loginId">登录人id</param>
		/// <param name="supportingParts"></param>
		/// <param name="cdNum"></param>
		/// <param name="orderNo">当前订单号</param>
		/// <param name="locateName">库位</param>
		/// <param name="isGroup">是否组托 1：是 0：不是</param>
		/// <returns></returns>
		public SetObjectDetail New_CheckCartonStatus(string cartonNo, bool isRejection, string groupNo, string loginId, string supportingParts, string cdNum, string orderNo, string locateName, string isGroup)
		{
			SetObjectDetail rfInfo = new SetObjectDetail();//返回消息

			List<SysStock_Extend> sysStocks = repository.GetStockByCartonNo(cartonNo, orderNo);
		   
			if (sysStocks.Count == 0)
			{
				//提示 " [M0090105] : 包装条码无效!";
				rfInfo.type = "FAIL";
				rfInfo.message = "[" + cartonNo + "] : 包装条码无效!";
				return rfInfo;
			}
			List<SysStock_Extend> _sysStocks = sysStocks.Where(a => a.CartonNo == cartonNo).ToList();
			if (_sysStocks.Count == 0)
			{
				// 提示 " [M0090105] : 不在订单列表中!";
				rfInfo.type = "FAIL";
				rfInfo.message = "[" + cartonNo + "] : 不在订单列表中!";
				return rfInfo;
			}
			if (_sysStocks[0].Status == 25)
			{
				rfInfo.type = "NUM";
				rfInfo.message = "[" + cartonNo + "] : 修改数量!";
				return rfInfo;
			}


			//List<SysStock_Extend> _sysStocks = null;

			//string json = JsonConvert.SerializeObject(__sysStocks);

			//_sysStocks = JsonConvert.DeserializeObject<List<SysStock_Extend>>(json);
			

			if (_sysStocks[0].Enabled == "N")
			{
				//提示  [M0090105]:包装条码已被冻结!
				rfInfo.type = "FAIL";
				rfInfo.message = "[" + cartonNo + "] : 包装条码已被冻结!";
				return rfInfo;
			}
			if (_sysStocks[0].Status != (int)WmsEnumUtil.StockStatus.WAITING_RECEIVE)//状态不为待收货
			{
				//提示 [M0090105]: 包装条码已收货!
				rfInfo.type = "FAIL";
				rfInfo.message = "[" + cartonNo + "] : 包装条码已收货!";
				return rfInfo;
			}

			//判断订单的实收数量，和应收数量
			List<WmsPurchaseDetail_Extend> WmsPurchaseDetail = repository.GetWmsPurchaseDetailByRecqty(orderNo, cartonNo);

			if (WmsPurchaseDetail != null && WmsPurchaseDetail.Count > 0)
			{
				SysPart_Extend sysPart = repository.GetPartExtendByPartId(_sysStocks[0].PartId.ToString());
				if (sysPart != null)
				{
					decimal _recqty= WmsPurchaseDetail[0].RecQty + _sysStocks[0].Qty;// 预计实收数量（实收数量+当前条码的数量）用于判断原由的实收数量+当前扫描的条码数量是否大于应收数量
					decimal revupperQty = 0;//收货上限比
					decimal revdowmperQty = 0;//收货下限比

					bool Issql = true;
					if (sysPart.revdowmper > 0 && sysPart.revdowmper != null && sysPart.revupper > 0 && sysPart.revupper != null)
					{
						revupperQty = WmsPurchaseDetail[0].Qty * (1 + sysPart.revupper);//最多收多少
						revdowmperQty = WmsPurchaseDetail[0].Qty * (1 - sysPart.revdowmper);//最少收多少
						if ( _recqty <= revupperQty)   //预计实收数量<=收货上限数量
                        {
							Issql = false;
						}

					}
					else if (sysPart.revupper > 0 && sysPart.revupper != null)//收货上限比。实收数量可以大于应收数量
					{
						revupperQty = WmsPurchaseDetail[0].Qty * (1 + sysPart.revupper);
						if ( _recqty <= revupperQty)//预计实收数量<=收货上限数量
                        {
							Issql = false;
						}

					}
					else if (sysPart.revdowmper > 0 && sysPart.revdowmper != null) //收货下限比。应收数量可以小于实收数量
					{
						revdowmperQty = WmsPurchaseDetail[0].Qty * (1 - sysPart.revdowmper);
						if (_recqty <= WmsPurchaseDetail[0].Qty)//预计实收数量<=应收数量
                        {
							Issql = false;
						}

					}
                    else //实收数量<=应收数量
                    {
						if (_recqty <= WmsPurchaseDetail[0].Qty)//预计实收数量<=应收数量
                        {
							Issql = false;
						}
					}
					if (Issql)
					{
						// 提示 " [M0090105] : 箱里的零件数量大于了应收数量!";
						rfInfo.type = "FAIL";
						rfInfo.message = "[" + cartonNo + "] : 箱里的零件数量大于了应收数量!";
						return rfInfo;
					}


				}


			}

			

			List<SysLocate> sysLocates = repository.GetLocateByName(locateName, loginId);
			if (sysLocates.Count == 0)
			{
				//提示 "[A1-2-2]:库位不存在，请重新输入!"
				rfInfo.type = "FAIL";
				rfInfo.message = "[" + locateName + "] : 库位不存在，请重新输入!";
				return rfInfo;
			}
			if (locateName.Length > 0)
			{
				List<SysStock> sysStockLocateCount = repository.GetStockQtyByLocate(locateName);

				if (sysLocates[0].LocateType == (int)WmsEnumUtil.LocateType.LINE_LOCATE || sysLocates[0].LocateType == (int)WmsEnumUtil.LocateType.COLLECT_LOCATE || sysLocates[0].LocateType == (int)WmsEnumUtil.LocateType.NC_LOCATE)
				{
					//提示  [A1-2-2]:该库位类型不匹配，请检查!
					rfInfo.type = "FAIL";
					rfInfo.message = "[" + locateName + "]:该库位类型不匹配，请检查!";
					return rfInfo;
				}

				if ((decimal)sysStockLocateCount[0].Qty >= sysLocates[0].LocateCapacity)
				{
					//提示  [A1-2-2]:该库位容量已满，请检查!
					rfInfo.type = "FAIL";
					rfInfo.message = "[" + locateName + "]:该库位容量已满，请检查!";
					return rfInfo;
				}
				//当前库位数量+当前箱条码的数量>= 当前库位容量
				if ((decimal)sysStockLocateCount[0].Qty + (decimal)_sysStocks[0].Qty >= sysLocates[0].LocateCapacity)
				{
					//提示  [A1-2-2]:该库位容量不足，请检查！
					rfInfo.type = "FAIL";
					rfInfo.message = "[" + locateName + "]:该库位容量不足，请检查！";
					return rfInfo;
				}
			}

			



			List<string> sqlStrings = new List<string>();
			List<DynamicParameters> parameterList = new List<DynamicParameters>();


			//更新收货主表状态：收货中
			//通过关联单号，将采购单的状态改为   30 处理中
			StringBuilder updateString2 = new StringBuilder(1024);
			updateString2.Append("update wms_purchase set order_status='30',update_userid=@update_userid,update_time=@update_time where order_no=@order_no  ");
			DynamicParameters updateStringparameters2 = new DynamicParameters();
			updateStringparameters2.Add("@order_no", _sysStocks[0].RefOrderNo);
			updateStringparameters2.Add("@update_userid", loginId);
			updateStringparameters2.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

			sqlStrings.Add(updateString2.ToString());
			parameterList.Add(updateStringparameters2);

			WmsPurchase WmsPurchaseByOrderoOn = repository.GetRejectionByOrderNoInfo(orderNo);

			//更改条码表状态
			string IsDirectWarehousing = "";
			if (_sysStocks[0].IsDirectWarehousing != null )//是否直入库
			{
				if (_sysStocks[0].IsDirectWarehousing == "1")
				{
					IsDirectWarehousing = "1";
				}
				else
				{
					IsDirectWarehousing = "0";
				}
				
			}
			else
			{
				IsDirectWarehousing = "0";
			}
			
			StringBuilder updateString = new StringBuilder(1024);
			updateString.Append("update sys_stock set status=@status,group_no=@group_no,qty=@qty,update_userid=@update_userid,update_time=@update_time,locate_id=@locate_id,locate_name=@locate_name,warehouse_id=@warehouse_id,warehouse_name=@warehouse_name,zone_id=@zone_id,zone_name=@zone_name,reveiceTime=@reveiceTime,pick_up_tab=@PickUpTab,is_create_qc_list=@IsCreateQcList,relevance_order=@RelevanceOrder  where carton_no=@carton_no ");
			DynamicParameters updateStringparameters = new DynamicParameters();
			if (IsDirectWarehousing !="1")//判断--箱条码的信息是否是直入库 ，不是直入库 条码状态改为25（待质检），是的话改为50（已上架）
			{
				updateStringparameters.Add("@status", (int)WmsEnumUtil.StockStatus.RECEIVED);
				updateStringparameters.Add("@IsCreateQcList", null);
			}
			else
			{//直入库
				updateStringparameters.Add("@status", (int)WmsEnumUtil.StockStatus.INSTOCKED);
				updateStringparameters.Add("@IsCreateQcList", (bool)false);
			}
			//updateStringparameters.Add("@IsDirectWarehousing", IsDirectWarehousing);//是否直入库 0：不是 1：是
			updateStringparameters.Add("@carton_no", cartonNo);
			updateStringparameters.Add("@RelevanceOrder", WmsPurchaseByOrderoOn== null?"": WmsPurchaseByOrderoOn.RefOrderNo);//来料订单号
			updateStringparameters.Add("@group_no", groupNo);
			updateStringparameters.Add("@qty", _sysStocks[0].Qty);
			updateStringparameters.Add("@locate_id", sysLocates[0].LocateId);
			updateStringparameters.Add("@locate_name", sysLocates[0].LocateName);
			updateStringparameters.Add("@warehouse_id", sysLocates[0].WarehouseId);
			updateStringparameters.Add("@warehouse_name", sysLocates[0].WarehouseName);//仓库名称
			updateStringparameters.Add("@zone_id", sysLocates[0].ZoneId);
			updateStringparameters.Add("@zone_name", sysLocates[0].ZoneName);//库区名称
			updateStringparameters.Add("@reveiceTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
			updateStringparameters.Add("@update_userid", loginId);
			updateStringparameters.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
			updateStringparameters.Add("@PickUpTab", "1");//捡料标记
			
			sqlStrings.Add(updateString.ToString());
			parameterList.Add(updateStringparameters);

			#region 新增 sys_stock_trans 事务表

			//新增 sys_stock_trans 事务表
			StringBuilder updateString4 = new StringBuilder(1024);
			updateString4.Append(@" INSERT INTO[dbo].[sys_stock_trans]
                                                    ([trans_code]
                                                    ,[carton_no]
                                                    ,[part_id]
                                                    ,[part_no]
                                                    ,[part_spec]
                                                    ,[src_locate_id]
                                                    ,[src_locate_name]
                                                    ,[dest_locate_id]
                                                    ,[dest_locate_name]
                                                    ,[old_qty]
                                                    ,[new_qty]
                                                    ,[trans_qty]
                                                    ,[old_status]
                                                    ,[new_status]
                                                    ,[old_qms_status]
                                                    ,[new_qms_status]
                                                    ,[unit]
                                                    ,[factory_id]
                                                    ,[factory_code]
                                                    ,[src_erp_warehouse]
                                                    ,[dest_erp_warehouse]
                                                    ,[src_warehouse_id]
                                                    ,[src_warehouse_name]
                                                    ,[dest_warehouse_id]
                                                    ,[dest_warehouse_name]
                                                    ,[src_zone_id]
                                                    ,[src_zone_name]
                                                    ,[dest_zone_id]
                                                    ,[dest_zone_name]
                                                    ,[enabled]
                                                    ,[update_userid]
                                                    ,[update_time]
                                                    ,[create_userid]
                                                    ,[create_time]
                                                    ,[guid])
                                                  VALUES (@trans_code,@carton_no,@part_id,@part_no,@part_spec,@src_locate_id ,@src_locate_name ,@dest_locate_id ,@dest_locate_name ,@old_qty 
                                        ,@new_qty ,@trans_qty ,@old_status ,@new_status ,@old_qms_status ,@new_qms_status ,@unit ,@factory_id ,@factory_code ,@src_erp_warehouse ,@dest_erp_warehouse ,@src_warehouse_id ,@src_warehouse_name ,@dest_warehouse_id ,@dest_warehouse_name ,@src_zone_id ,@src_zone_name ,@dest_zone_id ,@dest_zone_name ,@enabled ,@update_userid ,@update_time ,@create_userid ,@create_time ,@guid )");

			DynamicParameters updateStringparameters4 = new DynamicParameters();
			if (IsDirectWarehousing != "1")//判断--箱条码的信息是否是直入库 ，不是直入库 
			{
				updateStringparameters4.Add("@trans_code", (int)WmsEnumUtil.TransType.RC);
			}
			else
			{
				//直入库
				updateStringparameters4.Add("@trans_code", (int)WmsEnumUtil.TransType.INSTOCK);
			}
			//updateStringparameters4.Add("@trans_code", (int)WmsEnumUtil.TransType.QC);
			updateStringparameters4.Add("@carton_no", _sysStocks[0].CartonNo);
			updateStringparameters4.Add("@part_id", _sysStocks[0].PartId);
			updateStringparameters4.Add("@part_no", _sysStocks[0].PartNo);
			updateStringparameters4.Add("@part_spec", _sysStocks[0].PartSpec);
			updateStringparameters4.Add("@src_locate_id", 0);
			updateStringparameters4.Add("@src_locate_name", null);
			updateStringparameters4.Add("@dest_locate_id", sysLocates[0].LocateId);
			updateStringparameters4.Add("@dest_locate_name", sysLocates[0].LocateName);
			updateStringparameters4.Add("@old_qty", _sysStocks[0].Qty);
			updateStringparameters4.Add("@new_qty", _sysStocks[0].Qty);
			updateStringparameters4.Add("@trans_qty", _sysStocks[0].Qty);
			updateStringparameters4.Add("@old_status", 20);
			if (IsDirectWarehousing != "1")//判断--箱条码的信息是否是直入库 ，不是直入库 
			{
				updateStringparameters4.Add("@new_status", 25);
			}
			else
			{
				//直入库
				updateStringparameters4.Add("@new_status", 50);
			}

			updateStringparameters4.Add("@old_qms_status", "N");
			updateStringparameters4.Add("@new_qms_status", "N");
			updateStringparameters4.Add("@unit", _sysStocks[0].Unit);
			updateStringparameters4.Add("@factory_id", _sysStocks[0].FactoryId);
			updateStringparameters4.Add("@factory_code", _sysStocks[0].FactoryCode);
			updateStringparameters4.Add("@src_erp_warehouse", _sysStocks[0].ErpWarehouse);
			updateStringparameters4.Add("@dest_erp_warehouse", _sysStocks[0].ErpWarehouse);
			updateStringparameters4.Add("@src_warehouse_id", 0);
			updateStringparameters4.Add("@src_warehouse_name", "");
			updateStringparameters4.Add("@dest_warehouse_id", sysLocates[0].WarehouseId);
			updateStringparameters4.Add("@dest_warehouse_name", sysLocates[0].WarehouseName);
			updateStringparameters4.Add("@src_zone_id", 0);
			updateStringparameters4.Add("@src_zone_name", "");
			updateStringparameters4.Add("@dest_zone_id", sysLocates[0].ZoneId);
			updateStringparameters4.Add("@dest_zone_name", sysLocates[0].ZoneName);
			updateStringparameters4.Add("@enabled", _sysStocks[0].Enabled);
			updateStringparameters4.Add("@update_userid", loginId);
			updateStringparameters4.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
			updateStringparameters4.Add("@create_userid", loginId);
			updateStringparameters4.Add("@create_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
			updateStringparameters4.Add("@guid", Guid.NewGuid());

			sqlStrings.Add(updateString4.ToString());
			parameterList.Add(updateStringparameters4);


			#endregion


			#region 委外收货，新增 wms_subcontract_data

			//委外收货，新增 wms_subcontract_data
			List<WmsPurchase> dtSubcontract = repository.GetWmsPurchaseByOrderNo(orderNo);
			if (dtSubcontract.Count > 0)
			{
				if (dtSubcontract[0].Pstyp == 3)
				{
					StringBuilder updateString5 = new StringBuilder(1024);
					updateString5.Append(@" INSERT INTO[dbo].[wms_subcontract_data]
                                                    ([trans_code]
                                                     ,[part_id]
                                                     ,[part_no]
                                                     ,[part_spec]
                                                     ,[carton_no]
                                                     ,[qty]
                                                     ,[deal_qty]
                                                     ,[unit]
                                                     ,[deal_status]
                                                     ,[vendor_id]
                                                     ,[vendor_code]
                                                     ,[ref_order_no]
                                                     ,[factory_id]
                                                     ,[factory_code]
                                                     ,[enabled]
                                                     ,[create_userid]
                                                     ,[create_time]
                                                     ,[update_userid]
                                                     ,[update_time]
                                                     ,[guid]) 
                                                  VALUES ( @trans_code	 ,@part_id	 ,@part_no	 ,@part_spec	 ,@carton_no	 ,@qty	 ,@deal_qty	 ,@unit	 ,@deal_status	 ,@vendor_id	 ,@vendor_code	 ,@ref_order_no	 ,@factory_id	 ,@factory_code	 ,@enabled	 ,@create_userid
	                                         ,@create_time	 ,@update_userid	 ,@update_time	 ,@guid )");

					DynamicParameters updateStringparameters5 = new DynamicParameters();
					updateStringparameters5.Add("@trans_code", (int)WmsEnumUtil.PurchaseOrderType.SUB);
					updateStringparameters5.Add("@part_id", _sysStocks[0].PartId);
					updateStringparameters5.Add("@part_no", _sysStocks[0].PartNo);
					updateStringparameters5.Add("@part_spec", _sysStocks[0].PartSpec);
					updateStringparameters5.Add("@carton_no", _sysStocks[0].CartonNo);
					updateStringparameters5.Add("@qty", _sysStocks[0].Qty);
					updateStringparameters5.Add("@deal_qty", 0);
					updateStringparameters5.Add("@unit", _sysStocks[0].Unit);
					updateStringparameters5.Add("@deal_status", 10);
					updateStringparameters5.Add("@vendor_id", _sysStocks[0].VendorId);
					updateStringparameters5.Add("@vendor_code", _sysStocks[0].VendorCode);
					updateStringparameters5.Add("@ref_order_no", _sysStocks[0].RefOrderNo);
					updateStringparameters5.Add("@factory_id", _sysStocks[0].FactoryId);
					updateStringparameters5.Add("@factory_code", _sysStocks[0].FactoryCode);
					updateStringparameters5.Add("@enabled", _sysStocks[0].Enabled);
					updateStringparameters5.Add("@update_userid", loginId);
					updateStringparameters5.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
					updateStringparameters5.Add("@create_userid", loginId);
					updateStringparameters5.Add("@create_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
					updateStringparameters5.Add("@guid", Guid.NewGuid());

					sqlStrings.Add(updateString5.ToString());
					parameterList.Add(updateStringparameters5);
				}
			}

			#endregion





			int refNum = repository.SQLTransaction(sqlStrings, parameterList);

			if (refNum > 0)
			{
				//提示  操作成功!请继续扫描箱条码
				rfInfo.type = "OK";
				rfInfo.message = "OK" + _sysStocks[0].RefOrderNo; ;
				return rfInfo;
			}
			else
			{
				//提示  操作失败!
				rfInfo.type = "FAIL";
				rfInfo.message = "操作失败!请检查库存条码";
				return rfInfo;
			}

		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="cartonNo"></param>
		/// <param name="groupNo"></param>
		/// <param name="loginId"></param>
		/// <param name="orderNo"></param>
		/// <param name="locateName"></param>
		/// <param name="qtyReceived"></param>
		/// <returns></returns>
		public string Up_StockQty(string cartonNo, string groupNo, string loginId, string orderNo, string qtyReceived,string locateName)
		{

			return repository.Up_StockQty(cartonNo, groupNo, loginId, orderNo, qtyReceived, locateName);
		}

        /// </summary>
        /// <param name="cartonNo"></param>
        /// <param name="groupNo"></param>
        /// <param name="loginId"></param>
        /// <param name="orderNo"></param>
        /// <param name="locateName"></param>
        /// <param name="qtyReceived"></param>
        /// <returns></returns>
        public string Up_StockQtyPostBinding(string cartonNo, string groupNo, string loginId, string orderNo, string qtyReceived, string locateName)
        {

            return repository.Up_StockQtyPostBinding(cartonNo, groupNo, loginId, orderNo, qtyReceived, locateName);
        }

    }
}
